package com.javabase.y2021.m12.day1215;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

/**
 * @descriptions: ReadFile
 * @author: Tom
 * @date: 2021/12/15 上午 11:57
 * @version: 1.0
 */
public class Code03_ReadFile {
    public static void main(String[] args) {
        try(
                // 創建文件輸入流
                FileInputStream fis = new FileInputStream("\\docker-compose.yml");
                // 創建一個FileChannel
                FileChannel fileChannel = fis.getChannel()
                ){
            // 定義一個ByteBuffer對象，用於重複取水
            ByteBuffer bbuffer = ByteBuffer.allocate(256);
            // 將FileChannel的數據放入ByteBuffer中
            while(fileChannel.read(bbuffer) != -1){
                // 鎖定空白區
                bbuffer.flip();
                // 創建Charset對象
                Charset charset = Charset.forName("GBK");
                CharsetDecoder decoder = charset.newDecoder();
                // 將ByteBuffer的内容轉碼
                CharBuffer cbuffer = decoder.decode(bbuffer);
                System.out.println(cbuffer);
                bbuffer.clear();

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}









